import Vue from 'vue'
import axios from 'axios'
import VueRouter from 'vue-router'
import Logre from '@/views/Logre/LogRes.vue'
import Home from '@/views/Background/HOME/home.vue'
import Main from '@/views/Background/MAIN/MAin.vue'
import User from '@/views/Background/User/user.vue'
import Singer from '@/views/Background/Singer/singer.vue'
import AddSinger from '@/views/Background/Singer/AddSingers.vue'
import Song from '@/views/Background/Song/song.vue'
import AddSong from '@/views/Background/Song/AddSong.vue'
import SongList from '@/views/Background/SongList/songlist.vue'
import AddSongList from '@/views/Background/SongList/AddSongList.vue'
import ListSong from '@/views/Background/SongList/ListSong.vue'
import AddListSong from '@/views/Background/SongList/addlistsong.vue'
import AddUser from '@/views/Background/User/AddUser.vue'
import Layout from '@/views/Front/Layout/index.vue'
import FrontHome from '@/views/Front/HomeView/home.vue'
import FrontSinger from '@/views/Front/FrontSinger/index.vue'
import SearchList from '@/views/Front/Search/index.vue'
import MusicLyric from '@/views/Front/MusicLyric/index.vue'
import Personal from '@/views/Front/personal/person.vue'
import AddArticle from '@/views/AddArticle/index.vue'
import Plateform from '@/views/Front/platefrom/index.vue'
import ArticlDetl from '@/views/Front/platefrom/articldetial.vue'
import Article from '@/views/Front/personal/article.vue'
import ArticleManage from '@/views/Background/ArticleManage/index.vue'
import FrontSongList from '@/views/Front/SongList/SongList.vue'
import Songlistdetial from'@/views/Front/SongList/SongListdetail.vue'
import Tags from '@/views/Background/Tags/index.vue'
import AddTag from '@/views/Background/Tags/addtags.vue'
import SingerDetial from '@/views/Front/FrontSinger/singerdetial.vue'
import Banner from '@/views/Background/BannerManage/index.vue'
import AddBanner from '@/views/Background/BannerManage/AddBanner.vue'
import ElementUI from 'element-ui'
import About from '@/views/Front/About/index.vue'
Vue.use(VueRouter)

const routes = [
  {
    path:'/',
    component:Main,
    redirect:'/layout/fronthome',
    children:[
      {
        path:'home',
        component:Home
      },
      {
        path:'user',
        component:User
      },
      {
        path:'adduser',
        component:AddUser
      },
      {
        path:'singer',
        component:Singer
      },
      {
        path:'addsinger/:words?',
        component:AddSinger
      },
      {
        path:'song',
        component:Song
      },
      {
        path:'addsong',
        component:AddSong
      },
      {
        path:'songlist',
        component:SongList
      },
      {
        path:'addsonglist',
        component:AddSongList
      },
      {
        path:'listsong',
        component:ListSong
      },
      {
        path:'addlistsong',
        component:AddListSong
      },
      {
        path:'articleManage',
        component:ArticleManage
      },
      {
        path:'addarticle',
        component:AddArticle
      },
      {
        path:'tags',
        component:Tags
      },
      {
        path:'addtag',
        component:AddTag
      },
      {
        path:'banner',
        component:Banner
      },
      {
        path:'addBanner',
        component:AddBanner
      }
    ]
  },
  {
    path:'/layout',
    component:Layout,
    children:[
      {
        path:'fronthome',
        component:FrontHome
      },
      {
        path:'songlisdet',
        component:Songlistdetial
      },
      {
        path:'frontsinger',
        component:FrontSinger
      },
      {
        path:'singerdetial',
        component:SingerDetial
      },
      {
        path:'frontsonglist',
        component:FrontSongList
      },
      {
        path:'articldetl',
        component:ArticlDetl
      },
      {
        path:'searchlist',
        component:SearchList
      },
      {
        path:'musiclyric',
        component:MusicLyric
      },
      {
        path:'about',
        component:About
      },
      {
        path:'addarticle',
        component:AddArticle
      },
      {
        path:'platefrom',
        component:Plateform
      },
      {
        path:'personal',
        component:Personal,
        redirect:'/layout/personal/article',
        children:[
          {
            path:'article',
            component:Article
          }
        ]
      }
    ]
  },
  {
    path: '/logre',
    name: 'logre',
    component: Logre
  },
]

const router = new VueRouter({
  mode: 'history',
  base: process.env.BASE_URL,
  routes
})


const VueRouterPush = VueRouter.prototype.push
VueRouter.prototype.push = function push (to) {
  return VueRouterPush.call(this, to).catch(err => err)

}
const pathurl=['/home','/layout/personal']
router.beforeEach((to, from, next) => {
  // 是否需要拦截
    if(! pathurl.includes(to.path)){
        next()
        return
    }
    // 是否登录,登陆是否过期
    else{
        const user = JSON.parse(localStorage.getItem('honey-user') || '{}')
        if(!user){
          console.log("&&&&&&&&&&&&&&&&&")
          console.log(user)
          console.log("&&&&&&&&&&&&&&&&&")
            next('/logre')
        }
        else{
            axios({
                url:process.env.VUE_APP_BASEURL + '/User/checkToken',
                method:"get",
                headers:{
                  token:JSON.parse(localStorage.getItem('token') || '{}')
                }
            }).then((response)=>{
                if(response.data == true){
                  console.log("@@@@@@@@@@@@@@@@@@@@@@@@@@@")
                  console.log(response)
                    next()
                    return
                }
                else{
                  console.log("$$$$$$$$$$$$$$$$$$$$$$$$$$$$$")
                  console.log(response)
                  ElementUI.Message({
                      message:"登陆超时,请登录",
                      type:"error"
                  })
                  next('/logre')
                }
            })
        }
    }
})
export default router
